//app.js
var mta = require('utils/mta_analysis.js');
const mtjwxsdk = require("./utils/mtj-wx-sdk.js");
App({
  onLaunch: function () {
    console.log('onLaunch启动')
    // 展示本地存储能力
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
    wx.getSystemInfo({
      success: (result) => {
        this.globalData.cWidth = result.screenWidth
        this.globalData.cHeight = result.screenHeight
        let arr = ['iPhone X', 'iPhone 11', 'iPhone 12']
        wx.setStorageSync('safeArea', false)
        for (var index in arr) {
          if (result.model.indexOf(arr[index]) != -1) {
            wx.setStorageSync('safeArea', true)
          }
        }
      },
    })
    // 登录
    wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          wx.request({
            url: `https://edu.laigl.com/api/Wechat/get_user_info?code=${res.code}`,
            success: (req) => {
              let openid = req.data.data.openid
              wx.setStorageSync('openid', openid)
            },
            fail: (err) => {
              console.log(err)
            }
          })
        }
      }),
      mta.App.init({
        "appID": "500707997",
        "eventID": "500708008",
        "autoReport": true,
        "statParam": true,
        "ignoreParams": [],
        "statPullDownFresh": true,
        "statShareApp": true,
        "statReachBottom": true
      });
  },
  onHide() {
    //防止点名时，中途退出
    let pages = getCurrentPages();
    console.log(pages[pages.length - 1].__route__)
    if (pages[pages.length - 1].__route__ == 'pages/teacherName1/teacherName1') {
      wx.setStorageSync('isDian', 1)
    } else {
      wx.setStorageSync('isDian', 0)
    }
  },
  globalData: {
    userInfo: null,
    host: 'https://edu.laigl.com/api/', //正式服请求地址
    // host: 'http://edu.weilang.top/api/', //测试服请求地址
    token: '',
    type: '',
    tmplIds: [],
    cWidth: '',
    cHeight: '',
    isProfile: false
    // playState:0
  },
  onShow(options) {
    // console.log(options)
  }
})



Date.prototype.dateFormat = function (formats) {
  // formats格式包括
  // 1. Y-m-d
  // 2. Y-m-d H:i:s
  // 3. Y年m月d日
  // 4. Y年m月d日 H时i分
  formats = formats || 'Y-m-d';

  var zero = function (value) {
    if (value < 10) {
      return '0' + value;
    }
    return value;
  };

  var myDate = this ? new Date(this) : new Date();

  var year = myDate.getFullYear();
  var month = zero(myDate.getMonth() + 1);
  var day = zero(myDate.getDate());

  var hour = zero(myDate.getHours());
  var minite = zero(myDate.getMinutes());
  var second = zero(myDate.getSeconds());

  return formats.replace(/Y|m|d|H|i|s/ig, function (matches) {
    return ({
      Y: year,
      m: month,
      d: day,
      H: hour,
      i: minite,
      s: second
    })[matches];
  });
}